// pages/editAddress/editAddress.js
var app = getApp();
import WxValidate from '../../utils/WxValidate.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    receiverName: '',
    receiverPhone: '',
    zipCode: '',
    addrDetail: '',
    addrAreaId: '',
    addrArea: '',
    addrCity: '',
    addrCityId: '',
    addrProvince: '',
    addrProvinceId: '',
    addressList: [],
    multiArray: [],
    province: [],
    city: [],
    area: [],
    multiIndex: [0, 0, 0],
    isDefault: 0,
    ischangeAddr: false,
    id: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    this.setData({ id: options.id })
    let self = this;
    this.initValidate();
    wx.request({
      url: app.globalData.reqPath + 'base/address',
      header: {
        'authorization': app.globalData.authorization
      },
      data: {
        tree: 1,
        //level:1,
        limit: -1
      },
      success: function ({ data, header }) {
        if (data.code == 200) {
          //self.setData({addressList:data.data.list});
          self.getList(data.data.list)
        }
      }
    })
    wx.request({
      url: app.globalData.reqPath + 'shop/MembersAddress/' + id,
      header: {
        'authorization': app.globalData.authorization
      },
      success: function ({ data, header }) {
        if (data.code == 200) {
          self.setData({
            receiverName: data.data.receiverName,
            receiverPhone: data.data.receiverPhone,
            zipCode: data.data.zipCode,
            addrDetail: data.data.addrDetail,
            addrArea: data.data.addrArea,
            addrCity: data.data.addrCity,
            addrProvince: data.data.addrProvince,
            isDefault: data.data.isDefault
          })
          let n1 = 0, n2 = 0, n3 = 0;
          for (var i in self.data.addressList[0]) {
            if (self.data.addressList[0][i].areaName == data.data.addrProvince) {
              n1 = i;
            }
          }
          for (var j in self.data.addressList[1]) {
            if (self.data.addressList[1][j].areaName == data.data.addrCity) {
              n2 = j;
            }
          }
          for (var z in self.data.addressList[2]) {
            if (self.data.addressList[2][z].areaName == data.data.addrArea) {
              n3 = z;
            }
          }
          console.log(self.data.addressList);
        }
      }
    })
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindRegionChange: function (e) {//选择地区
    console.log(e.detail.value)
    this.setData({ region: e.detail.value })
  },
  switchChange: function (e) {//选择是否默认
    console.log(e.detail.value)
    if (e.detail.value) {
      this.setData({ isDefault: 1 })
    } else {
      this.setData({ isDefault: 0 })
    }
  },
  getList: function (list) {
    let p = [];
    let c = [];
    let a = [];
    list.forEach(function (item) {
      p.push({ areaId: item.areaId, areaName: item.areaName });
    })
    //   console.log(list[0])
    for (var i in list[0].child) {
      c.push({ areaId: list[0].child[i].areaId, areaName: list[0].child[i].areaName })
      for (var j in list[0].child[i].child) {
        a.push({ areaId: list[0].child[i].child[j].areaId, areaName: list[0].child[i].child[j].areaName })
      }
    }
    this.setData({ addressList: [p, c, a], province: p })
  },
  bindMultiPickerColumnChange: function (e) {
    console.log(e.detail.column + ":" + e.detail.value)
    let column = e.detail.column;
    let index = parseInt(e.detail.value);
    let self = this;
    if (column == 0) {
      this.setData({ ischangeAddr: true })
      this.setData({ multiIndex: [index, 0, 0] })
      wx.request({
        url: app.globalData.reqPath + 'base/address',
        header: {
          'authorization': app.globalData.authorization
        },
        data: {
          parentId: this.data.province[index].areaId,
          limit: -1,
        },
        success: function ({ data, header }) {
          self.setData({ city: data.data.list });
          console.log(data.data.list[0])
          let pid = data.data.list[0].areaId;
          wx.request({
            url: app.globalData.reqPath + 'base/address',
            header: {
              'authorization': app.globalData.authorization
            },
            data: {
              parentId: pid,
              limit: -1,
            },
            success: function ({ data, header }) {
              self.setData({ area: data.data.list });
              self.setData({ addressList: [self.data.province, self.data.city, data.data.list] });
            }
          })
          self.setData({ addressList: [self.data.province, data.data.list, self.data.area] })
        }
      })
    } else if (column == 1) {
      this.setData({ multiIndex: [this.data.multiIndex[0], index, this.data.multiIndex[2]] })
      console.log(this.data.city[index].areaId)
      wx.request({
        url: app.globalData.reqPath + 'base/address',
        header: {
          'authorization': app.globalData.authorization
        },
        data: {
          parentId: this.data.city[index].areaId,
          limit: -1,
        },
        success: function ({ data, header }) {
          // self.setData({ areaId: data.data.list})
          console.log(data.data.list)
          self.setData({ addressList: [self.data.province, self.data.city, data.data.list] })
        }
      })

    } else if (column == 2) {
      this.setData({ multiIndex: [this.data.multiIndex[0], this.data.multiIndex[1], index] })
    }
  },
  subedit: function (e) {
    let parames = e.detail.value
    if (!this.WxValidate.checkForm(parames)) {
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
      return false;
    }
    let addrProvince, addrCity, addrArea;
    if (this.data.ischangeAddr) {
      addrProvince: this.data.addrProvince;
      addrCity: this.data.addrCity;
      addrArea: this.data.addrArea
    } else {
      addrProvince: this.data.addressList[0][this.data.multiIndex[0]].areaName;
      addrCity: this.data.addressList[1][this.data.multiIndex[1]].areaName;
      addrArea: this.data.addressList[2][this.data.multiIndex[2]].areaName;
    }
    let data = {
      receiverPhone: parames.receiverPhone,
      receiverName: parames.receiverName,
      addrProvince: addrProvince,
      addrCity: addrCity,
      addrArea: addrArea,
      addrDetail: parames.addrDetail,
      isDefault: this.data.isDefault,
      zipCode: parames.zipCode
    }
    console.log(data)
    wx.request({
      url: app.globalData.reqPath + 'shop/MembersAddress/' + this.data.id,
      method: 'PUT',
      data: data,
      header: {
        'authorization': app.globalData.authorization
      },
      success: function ({ data, header }) {
        if (header.authorization && header.authorization != app.globalData.authorization) {
          app.globalData.authorization = header.authorization;
        }
        console.log(data)
        if (data.code == 200) {
          wx.showToast({
            title: '修改成功',
            icon: 'none',
            success: function () {
              wx.redirectTo({
                url: '/pages/addressList/addressList?r=3',
              })
            }
          })
        }
      }
    })

  },
  //表单验证
  initValidate() {
    const rules = {
      receiverName: {
        required: true,
      },
      receiverPhone: {
        required: true,
        tel: true
      },
      addrDetail: {
        required: true
      }
    }
    const messages = {
      receiverName: {
        required: '请输入联系人',
      },
      receiverPhone: {
        required: "请输入手机号码",
        tel: "请输入正确的手机号码",
      },
      addrDetail: {
        required: "请输入详细地址",
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  showModal: function (error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  }  
})